Passed
Pull Request — master (#220)
by
unknown
02:04
created

ArrayUtils.flatMap   A

Complexity

Conditions 2

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 2
1
export class ArrayUtils {
2
  public static range(start: number, end: number, step: number = 1) {
3
    function* generateRange() {
4
      let x = start - step;
5
      while (x <= end - step) {
6
        yield (x += step);
7
      }
8
    }
9
10
    return {
11
      [Symbol.iterator]: generateRange
12
    };
13
  }
14
15
  public static flatMap<T, V>(items: T[], fn: (x: T) => V[]): V[] {
16
    const arr = [];
17
    for (let item of items) {
18
      arr.push(...fn(item));
19
    }
20
    return arr;
21
  }
22
}
23